package com.ivo.controller.pm;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ivo.model.pm.FollowUpRequisition;
import com.ivo.service.hr.HrService;
import com.ivo.service.pm.PmService;
import com.ivo.util.Util;

@Controller
public class PmController
{
	private PmService pmService;
	private HrService hrService;

	public PmController() {
	}

	@Autowired
	public PmController( PmService pmService, HrService hrService ) {
		this.pmService = pmService;
		this.hrService = hrService;
	}

	@RequestMapping( "/pm/getFURList.htm" )
	public ModelAndView getFURList( HttpServletRequest request, HttpServletResponse response, ModelMap modelMap ) throws ServletException, IOException {
		StringBuffer returnStr = new StringBuffer();

		String start = "0";
		String limit = "50";
		String query = request.getParameter("query");

		List<FollowUpRequisition> lists = pmService.getFurByRD(query);
		int nSize = 0;
		nSize = lists.size();

		if( lists.size() > 0 ) {
			returnStr.append("{'totalProperty':'" + nSize + "','root':[");
			int index = Integer.parseInt(start);
			int pageSize = Integer.parseInt(limit);

			int returnInt;
			if( nSize >= ( pageSize + index ) )
				returnInt = pageSize + index;
			else
				returnInt = nSize;

			for( int i = index; i < returnInt; i++ ) {

				FollowUpRequisition fur = lists.get(i);

				String strRequisitioner = fur.getRequisitioner_FK();
				String strMemo = fur.getMemo();
				strRequisitioner = hrService.getEmployee(strRequisitioner).getName() + " / " + hrService.getEmployee(strRequisitioner).getName_EN();

				if( strMemo.length() > 50 ) {
					strMemo = strMemo.substring(0, 50) + "  ...";
				}

				returnStr.append("{'followUpRequisition_ID':'");
				returnStr.append(fur.getFollowUpRequisition_ID());
				returnStr.append("','trackingNumber':'");
				returnStr.append(fur.getTrackingNumber());
				returnStr.append("','requisitioner':'");
				returnStr.append(strRequisitioner);
				returnStr.append("','category_FK':'");
				returnStr.append(fur.getCategory_FK());
				returnStr.append("','project_FK':'");
				returnStr.append(fur.getProject_FK());
				returnStr.append("','product_FK':'");
				returnStr.append(fur.getProduct_FK());
				returnStr.append("','version':'");
				returnStr.append(fur.getVersion());
				returnStr.append("','phase_FK':'");
				returnStr.append(fur.getPhase_FK());
				returnStr.append("','memo':'");
				returnStr.append(Util.saveStr(strMemo));
				returnStr.append("'}");

				if( i == nSize - 1 ) {
				} else if( i != pageSize + index - 1 ) {
					returnStr.append(",");
				}
			}
			returnStr.append("]}");
		} else {
			returnStr.append("{'totalProperty':'0','root':[]}");
		}

		// System.out.println(returnStr.toString());
		return new ModelAndView("ajax", "outString", returnStr.toString());
	}

}
